package com.tj.collection.application;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.qianli.cashmama.product.facade.result.*;
import com.tj.collection.integration.ProductIntegrationService;
import com.tj.collection.integration.dto.AppBusinessProductDTO;
import com.tj.collection.integration.dto.BusinessProductDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WatsonQiu
 * Date: 2017/7/25
 * Time: 下午8:05
 */
@Component
public class ProductApplication {
    private static final Log log = LogFactory.getLog(ProductApplication.class);

    @Autowired
    private ProductIntegrationService productIntegrationService;

    /**
     * 根据产品编码获取产品信息
     *
     * @param productCodeList
     * @return
     */
    public Map<String, Product> getProductInfo(List<String> productCodeList) {
        Map<String, Product> productMap = new HashMap<>();
        for (String productCode : productCodeList) {
            try {
                Response<Product> productResponse = productIntegrationService.getProductDetail(productCode, true);
                if (!productResponse.isSuccess()) {
                    log.error("根据产品编码获取产品信息异常");
                    return new HashMap<>();
                }
                Product product = productResponse.getData();
                productMap.put(productCode, product);
            } catch (Exception e) {
            }
        }
        return productMap;
    }

    /**
     * 获取产品类目列表
     *
     * @return
     */
    public List<ProductCategory> getProductCategoryList() {
        Response<List<ProductCategory>> response = productIntegrationService.getAllActiveProductCategory();
        if (!response.isSuccess()) {
            log.error("获取产品类目列表异常");
            return null;
        }
        List<ProductCategory> productCategoryList = response.getData();
        return productCategoryList;
    }

    /**
     * 获取产品类目列表
     *
     * @return
     */
    public List<ProductCategory> getProductCategoryList(String merchantCode) {
        Response<List<ProductCategory>> response = productIntegrationService.getAllActiveProductCategoryByMerchantCode(merchantCode);
        if (!response.isSuccess()) {
            log.error("获取产品类目列表异常");
            return null;
        }
        List<ProductCategory> productCategoryList = response.getData();
        return productCategoryList;
    }

    public List<BusinessProduct> getBusinessProducts() {
        return productIntegrationService.getBusinessProductList();
    }

    public List<AppBusinessProduct> getAppBusinessProducts(Integer position) {
        return productIntegrationService.getAppBusinessProductList(position);
    }

    public boolean addBusinessProduct(BusinessProductDTO businessProductDTO) {
        return productIntegrationService.addBusinessProduct(businessProductDTO);
    }

    public boolean deleteBusinessProduct(Long id) {
        AppBusinessProductDTO appBusinessProduct = new AppBusinessProductDTO();
        appBusinessProduct.setBusinessProductId(id);
        List<AppBusinessProduct> appBusinessProductList = productIntegrationService.getAppBusinessProductList(appBusinessProduct);
        if (CollectionUtils.isNotEmpty(appBusinessProductList)) {
            throw new ApplicationException("this product exist in app business product");
        }
        return productIntegrationService.deleteBusinessProduct(id);
    }

    public boolean addAppBusinessProduct(AppBusinessProductDTO appBusinessProductDTO) {
        log.info("== addAppBusinessProduct appBusinessProductDTO :{} ==", JSONObject.toJSONString(appBusinessProductDTO));
        if (appBusinessProductDTO.getId() == null) {
            List<AppBusinessProduct> appBusinessProductList = productIntegrationService.getAppBusinessProductList(appBusinessProductDTO.getPosition(), appBusinessProductDTO.getBusinessProductId());
            if (CollectionUtils.isNotEmpty(appBusinessProductList)) {
                throw new ApplicationException("the position of product is exist");
            }
        }
        return productIntegrationService.addAppBusinessProduct(appBusinessProductDTO);
    }

    public AppBusinessProducts getAppBusinessProductsByPage(Integer position, Page page) {
        Map<String, Object> paramMap = new HashMap<>();
        if (position != null) {
            paramMap.put("position", position);
        }
        return productIntegrationService.getAppBusinessProductListByPage(paramMap, page);
    }
}
